<template>
  <div class="login-wrap">
    <el-form
      class="login-form"
      label-position="top"
      label-width="80px"
      :model="formdata">
      <h2>用户登录</h2>
      <el-form-item label="用户名">
        <el-input v-model="formdata.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="formdata.password"></el-input>
      </el-form-item>
      <el-button class="login-btn" type="primary" @click.prevent="handleLogin()">登录</el-button>
    </el-form>
  </div>
</template>
<script>
  export default {
    data(){
      return{
        formdata:{
          username:'',
          password:'',
        }
      }
    },
    methods:{
      //登录请求
      async handleLogin(){
        /*this.$http.post('login',this.formdata).then(res=>{
          console.log(res,'登录接口');
          //解构赋值的写法
          const {
            data,
            meta:{msg,status}
          } = res.data
          //登录成功
          /!**
           * 成功
           *1.跳转home
           * 2.提示成功
           * 失败
           * 1.提示信息
           *
          * *!/
          if(status===200){
            this.$router.push({name:'home'})
            this.$message.success(msg)
          }else {
            this.$message.error(msg)
          }
        })*/
        //es7 async+await 简化异步操作
        const res = await this.$http.post('login',this.formdata)
          console.log(res,'登录接口');
          //解构赋值的写法
          const {
            data,
            meta:{msg,status}
          } = res.data
          //登录成功
          /**
           * 成功
           * 0.保存用户token
           *1.跳转home
           * 2.提示成功
           * 失败
           * 1.提示信息
           *
          * */
          if(status===200){
            //保存token
            localStorage.setItem('token',data.token)
            this.$router.push({name:'home'})
            this.$message.success(msg)
          }else {
            this.$message.error(msg)
          }
      }
    }
  }
</script>
<style>
  .login-wrap{
    height: 100%;
    background-color: #324152;
    /*弹性盒布局：让登录窗口居中css*/
    display: flex;
    justify-content: center;
    align-items: center;
    /*让登录窗口居中css*/
  }
  .login-wrap .login-form{
    width: 400px;
    background-color: #fff;
    border-radius: 5px;
    padding: 30px;
  }
  .login-wrap .login-btn{
    width: 100%;
  }
</style>
